import redis
import json

# 创建 Redis 连接
r_da1 = redis.Redis(host='172.31.120.26', port=6379, db=1, password='dev#redis@b26210ae')  # 请根据您的实际 Redis 配置修改
# 读取键为 'key1' 的值
key1 = 'str:gud:uid:phone:13367185684'
phone_value = r_da1.get(key1)
phone_value_str = ''
if phone_value:
    phone_value_str = phone_value.decode('utf-8')
    print(f"键 '{key1}' 的值为: {phone_value_str}")  # 对获取的值进行解码
else:
    print(f"键 '{key1}' 不存在")

# 创建 Redis 连接
key2 = f'str:gud:uinfo:{phone_value_str}'
user_info = ''
# 读取键为 'key1' 的值
uinfo_value = r_da1.get(key2)
if uinfo_value:
    user_info = uinfo_value.decode('utf-8')

    print(f"键 '{key2}' 的值为: {user_info}")  # 对获取的值进行解码
else:
    print(f"键 '{key2}' 不存在")


def reset_phone_fields(json_data):
    data = json.loads(json_data)

    def process(data):
        if isinstance(data, dict):
            for key, value in list(data.items()):  # 使用 list 来避免在迭代时修改字典导致的错误
                if key == 'phone':
                    data[key] = ''
                else:
                    process(value)
        elif isinstance(data, list):
            for item in data:
                process(item)

    process(data)
    return json.dumps(data)


remove_phone_user_info = reset_phone_fields(user_info)
print(remove_phone_user_info)
# 将修改后的数据更新到缓存
r_da1.set(key2, remove_phone_user_info)
# 删除手机号那个缓存
r_da1.delete(key1)


uinfo_value = r_da1.get(key2)
if uinfo_value:
    user_info = uinfo_value.decode('utf-8')
    print(f"键 '{key2}' 的值为: {user_info}")  # 对获取的值进行解码
else:
    print(f"键 '{key2}' 不存在")
uinfo_value = r_da1.get(key1)
if uinfo_value:
    user_info = uinfo_value.decode('utf-8')
    print(f"键 '{key1}' 的值为: {user_info}")  # 对获取的值进行解码
else:
    print(f"键 '{key1}' 不存在")